# Copyright 2010-2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Utilities for Guile-scripting related tests.

# Guile doesn't print the 0x prefix on hex numbers.
set ghex {[0-9a-f]+}

# Return a 1 for configurations that do not support Guile scripting.

proc skip_guile_tests {} {
    global gdb_prompt

    gdb_test_multiple "guile (display \"test\\n\")" "verify guile support" {
	-re "Undefined command.*$gdb_prompt $" {
	    unsupported "Guile not supported."
	    return 1
	}
	-re "not supported.*$gdb_prompt $" {
	    unsupported "Guile support is disabled."
	    return 1
	}
	-re "$gdb_prompt $" {}
    }

    return 0
}

# Run a command in GDB, and report a failure if a Scheme exception is thrown.
# If report_pass is true, report a pass if no exception is thrown.
# This also catches the "Undefined command" error that happens if the user
# passes, e.g., "(print foo)" instead of "guile (print foo)".

proc gdb_scm_test_silent_cmd { cmd name {report_pass 1} } {
    global gdb_prompt

    gdb_test_multiple $cmd $name {
	-re "Backtrace.*$gdb_prompt $" { fail $name }
	-re "ERROR.*$gdb_prompt $"     { fail $name }
	-re "Undefined command: .*$gdb_prompt $" { fail $name }
	-re "$gdb_prompt $"            { if $report_pass { pass $name } }
    }
}

# Usage: gdb_test_multiline NAME INPUT RESULT {INPUT RESULT} ...
# Run a test named NAME, consisting of multiple lines of input.
# After each input line INPUT, search for result line RESULT.
# Succeed if all results are seen; fail otherwise.
# FIXME: Move to gdb.exp and remove Python's gdb_py_test_multiple.

proc gdb_test_multiline { name args } {
    global gdb_prompt
    foreach {input result} $args {
	if {[gdb_test_multiple $input "$name - $input" {
	    -re "\[\r\n\]*($result)\[\r\n\]+($gdb_prompt | *>)$" {
		pass "$name - $input"
	    }
	}]} {
	    return 1
	}
    }
    return 0
}

# Load Scheme file FILE_NAME.
# TEST_NAME can be used to specify the name of the test,
# otherwise a standard test name is provided.
#
# Note: When Guile loads something and auto-compilation is enabled
# (which is useful and the default), then the first time a file is loaded
# Guile will compile the file and store the result somewhere
# (e.g., $HOME/.cache/guile).  Output of the compilation process will
# appear in gdb.log.  But since Guile only does this when necessary
# don't be confused if you don't always see it - Guile just skipped it
# because it thought it was unnecessary.

proc gdb_scm_load_file { file_name {test_name ""} } {
    if { $test_name == "" } {
	set test_name "guile (load \"[file tail $file_name]\")"
    }
    # Note: This can produce output if Guile compiles the file.
    gdb_scm_test_silent_cmd "guile (load \"$file_name\")" $test_name
}

# Install various utilities in Guile to simplify tests.
#
# print - combination of display + newline

proc gdb_install_guile_utils { } {
    # Define utilities in Guile to save needing (newline) all the time,
    # and in the case of "print" add a prefix to help erroneous passes.
    gdb_test_no_output "guile (define (print x) (format #t \"= ~A\" x) (newline))"
    gdb_test_no_output "guile (define (raw-print x) (format #t \"= ~S\" x) (newline))"
}

# Install the gdb module.

proc gdb_install_guile_module { } {
    gdb_test_no_output "guile (use-modules (gdb))"
}

# Wrapper around runto_main that installs the guile utils and module.
# The result is the same as for runto_main.

proc gdb_guile_runto_main { } {
    if ![runto_main] {
	fail "Can't run to main"
	return 0
    }

    gdb_install_guile_utils
    gdb_install_guile_module

    return 1
}
